﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Text;

namespace Company.StudyClub.BLL.Cookie
{
    public class CookieManager
    {
        public static string ReadCookie(string cookieName)
        {
            return ReadCookie(HttpContext.Current, cookieName);
        }
        public static string ReadCookie(HttpContext objContext, string cookieName)
        {
            if (objContext.Request.Cookies[cookieName] == null)
                return null;
            return objContext.Request.Cookies[cookieName].Value.Trim();
        }

        public static void WriteCookie(string cookieName, string cookieValue, DateTime expiredDate)
        {
            WriteCookie(HttpContext.Current, cookieName, cookieValue, expiredDate);
        }
        public static void WriteCookie(HttpContext objContext, string cookieName, string cookieValue, DateTime expiredDate)
        {
            System.Web.HttpCookie objC;
            if (objContext.Request.Cookies[cookieName] == null)
                objC = new HttpCookie(cookieName, cookieValue);
            else
                objC = objContext.Request.Cookies[cookieName];
            objC.Value = cookieValue;
            if (expiredDate != DateTime.Parse("1900-1-1"))
            {
                objC.Expires = expiredDate;
            }
            objContext.Response.Cookies.Add(objC);
        }

        public static void ClearCookie(string cookieName)
        {
            ClearCookie(HttpContext.Current, cookieName);
        }
        public static void ClearCookie(HttpContext objContext, string cookieName)
        {
            if (objContext.Request.Cookies[cookieName] != null)
            {
                System.Web.HttpCookie objC = objContext.Request.Cookies[cookieName];
                objC.Expires = DateTime.Now.AddHours(-1);
                objContext.Response.Cookies.Add(objC);
            }
        }
    }
}
